package com.sojson.project.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.constant.Constant;
import com.sojson.constant.ConstantMessageSystemTemplate;
import com.sojson.constant.IpConfig;
import com.sojson.constant.Lock;
import com.sojson.enums.EBalanceCount;
import com.sojson.enums.EBalanceTotalType;
import com.sojson.enums.EBalanceType;
import com.sojson.enums.EEarnestMoneyStatus;
import com.sojson.enums.EMessageSystemType;
import com.sojson.enums.EOrderDepositDeductType;
import com.sojson.enums.EOrderDepositIsToBrokerage;
import com.sojson.enums.EOrderDepositStatus;
import com.sojson.enums.EPostApplyPostsStatus;
import com.sojson.enums.EPostType;
import com.sojson.enums.core.EPayStatus;
import com.sojson.enums.core.ERefundsStatus;
import com.sojson.project.dao.OrderDepositDao;
import com.sojson.project.dao.PostApplyDao;
import com.sojson.project.entity.po.BalanceLog;
import com.sojson.project.entity.po.Deposit;
import com.sojson.project.entity.po.OrderDeposit;
import com.sojson.project.entity.vo.OrderDepositVo;
import com.sojson.project.entity.vo.PostApplyVo;
import com.sojson.project.service.BalanceLogService;
import com.sojson.project.service.DepositService;
import com.sojson.project.service.MessageSystemService;
import com.sojson.project.service.OrderDepositService;
import com.sojson.project.sys.user.dao.UserDao;
import com.sojson.project.sys.user.entity.po.User;
import com.sojson.util.ArithUtil;
import com.sojson.util.DateUtil;
import com.sojson.util.ExpUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.id.IdUtil;
import com.sojson.util.pay.PayUtil;
import com.sojson.util.pay.wx.constant.WxConstant;
import com.sojson.util.token.TokenUtil;

/**
 * 诚意金订单表Service实现类
 *
 * @author liu
 * @date 2021-04-29
 */
@Service
public class OrderDepositServiceImpl implements OrderDepositService {

    @Lazy
    @Autowired
    private OrderDepositServiceImpl orderDepositService;
    @Autowired
    private DepositService depositService;
    @Autowired
    private MessageSystemService messageSystemService;
    @Autowired
    private BalanceLogService balanceLogService;
    @Resource
    private OrderDepositDao orderDepositDaoImpl;
    @Resource
    private UserDao userDaoImpl;
    @Resource
    private PostApplyDao postApplyDaoImpl;

    /**
     * 单条插入
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int insert(OrderDeposit orderDeposit) {
        int insert = orderDepositDaoImpl.insert(orderDeposit);
        return insert;
    }

    /**
     * 批量插入
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int insertList(OrderDeposit orderDeposit) {
        int insertList = orderDepositDaoImpl.insertList(orderDeposit.getOrderDeposits());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int deleteByDto(OrderDeposit orderDeposit) {
        int deleteByDto = orderDepositDaoImpl.deleteByDto(orderDeposit);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     *
     * @return
     */
    @Override
    public int countAll() {
        int countAll = orderDepositDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<OrderDeposit> findAll() {
        List<OrderDeposit> findAll = orderDepositDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int countByDto(OrderDeposit orderDeposit) {
        int countByDto = orderDepositDaoImpl.countByDto(orderDeposit);
        return countByDto;
    }

    /**
     * 根据对象查询
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public List<OrderDeposit> findByDto(OrderDeposit orderDeposit) {
        List<OrderDeposit> findByDto = orderDepositDaoImpl.listByDto(orderDeposit);
        return findByDto;
    }

    /**
     * 根据对象查询
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public List<OrderDeposit> findAppByDto(OrderDeposit orderDeposit) {
        List<OrderDeposit> findByDto = orderDepositDaoImpl.listAppByDto(orderDeposit);
        return findByDto;
    }

    /**
     * 根据对象查询
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public List<OrderDepositVo> list(OrderDeposit orderDeposit) {
        List<OrderDepositVo> findByDto = orderDepositDaoImpl.list(orderDeposit);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = orderDepositDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteByIdList(List<Integer> ids) {
        int deleteByIdList = orderDepositDaoImpl.deleteByIdList(ids);
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int updateById(OrderDeposit orderDeposit) {
        int updateById = orderDepositDaoImpl.updateById(orderDeposit);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int updateStaticByIdList(OrderDeposit orderDeposit) {
        int updateStaticByIdList = orderDepositDaoImpl.updateStaticByIdList(orderDeposit);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int updateChangeByIdList(OrderDeposit orderDeposit) {
        int updateChangeByIdList = orderDepositDaoImpl.updateChangeByIdList(orderDeposit.getOrderDeposits());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int updatePartialById(OrderDeposit orderDeposit) {
        int updatePartialById = orderDepositDaoImpl.updatePartialById(orderDeposit);
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int updateStaticPartialByIdList(OrderDeposit orderDeposit) {
        int updateStaticPartialByIdList = orderDepositDaoImpl.updateStaticPartialByIdList(orderDeposit);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int updateChangePartialByIdList(OrderDeposit orderDeposit) {
        int updateChangePartialByIdList =
            orderDepositDaoImpl.updateChangePartialByIdList(orderDeposit.getOrderDeposits());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public OrderDeposit findById(String id) {
        OrderDeposit findById = orderDepositDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     *
     * @param ids
     * @return
     */
    @Override
    public List<OrderDeposit> findByIdList(List<Integer> ids) {
        List<OrderDeposit> findByIdList = orderDepositDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public List<Integer> findIdByDto(OrderDeposit orderDeposit) {
        List<Integer> findIdByDto = orderDepositDaoImpl.findIdByDto(orderDeposit);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     */
    @Override
    public void clear() {
        orderDepositDaoImpl.clear();
    }

    /**
     * 下单
     *
     * @return
     * @throws Exception
     */
    @Override
    public String orders(String payPwd) throws Exception {
        synchronized (Lock.ORDER_DEPOSIT_STATUS) {
            return orderDepositService.ordersGo(payPwd);
        }
    }

    @Transactional(rollbackFor = Throwable.class)
    public String ordersGo(String payPwd) throws Exception {
        OrderDeposit orderDeposit;
        // 用户ID
        String memberId = TokenUtil.getToken().getUser().getId();

        // 查看用户诚意金状态
        User findById = userDaoImpl.findById(memberId);
        if (findById.getEarnestMoneyStatus() == EEarnestMoneyStatus.YES.getCode()) {
            ExpUtil.throwEx("诚意金已支付！");
        }
        if (StringUtil.isBlankObject(findById.getOpenid())) {
            ExpUtil.throwEx("未绑定微信,请先绑定微信！");
        }

        // 查找是否存在未付款的
        OrderDeposit findByDto = new OrderDeposit();
        findByDto.setMid(memberId);
        findByDto.setStatus(EOrderDepositStatus.PAYMENT_NO.getCode());
        findByDto.setIsPay(EPayStatus.NO.getCode());
        findByDto.setOrderByColumn("created_at");
        findByDto.setThisTime(new Date(DateUtil.nowLong() - Constant.ORDER_DEPOSIT_PAY_TIME + Constant.MILLIS_DAY));
        List<OrderDeposit> findByDtoList = orderDepositDaoImpl.listByDto(findByDto);

        // 获取诚意金
        Deposit info = depositService.getInfo();
        // 如果存在未支付的就用这一单支付,否则重新建一单
        if (StringUtil.isBlankObject(findByDtoList) || findByDtoList.size() == 0
            || info.getTotal().doubleValue() != findByDtoList.get(0).getOrderTotal().doubleValue()) {
            orderDeposit = new OrderDeposit();
            // 订单号
            String uuid = IdUtil.uuid();
            // 获取价格
            BigDecimal pri = info.getTotal();

            orderDeposit.insertInit();
            orderDeposit.setMid(memberId);
            orderDeposit.setOrderSn(uuid);
            orderDeposit.setOrderTotal(pri);
            orderDeposit.setPayTotal(pri);

            // 添加订单
            orderDepositDaoImpl.insert(orderDeposit);
        } else {
            orderDeposit = findByDtoList.get(0);
        }

        return orderDeposit.getId();
    }

    /**
     * 通过订单号获取诚意金订单
     *
     * @param orderSn
     * @return
     */
    @Override
    public OrderDeposit getInfoByOrderSn(String orderSn) {
        return orderDepositDaoImpl.getInfoByOrderSn(orderSn);
    }

    /**
     * 取消订单
     *
     * @param id 订单ID
     * @return
     * @throws Exception
     */
    @Override
    public int cancel(String id) throws Exception {
        synchronized (Lock.ORDER_DEPOSIT_STATUS) {
            return orderDepositService.cancelGo(id);
        }
    }

    @Transactional(rollbackFor = Throwable.class)
    public int cancelGo(String id) throws Exception {
        int updatePartialById = 0;
        OrderDeposit findById = null;
        // 查询订单
        findById = orderDepositDaoImpl.findById(id);

        // 查看订单是否存在
        if (StringUtil.isBlankObject(findById)) {
            ExpUtil.throwEx("订单不存在！");
        }

        // 查看订单是否可以取消
        if (EOrderDepositStatus.PAYMENT_NO.getCode() != findById.getStatus()) {
            ExpUtil.throwEx("订单状态不是待付款！");
        }

        // 修改订单状态
        OrderDeposit ord = new OrderDeposit();
        ord.setId(findById.getId());
        ord.setStatus(EOrderDepositStatus.CANCEL_YES.getCode());
        updatePartialById = orderDepositDaoImpl.updatePartialById(ord);
        if (updatePartialById < 1) {
            ExpUtil.throwEx("订单状态修改失败！");
        }
        return updatePartialById;
    }

    /**
     * 定时任务自动关闭订单
     *
     * @param nowDate
     * @return
     */
    @Override
    public int cancelAuto(Date nowDate) {
        return orderDepositDaoImpl.cancelAuto(nowDate);
    }

    /**
     * 退诚意金
     *
     * @return
     * @throws Exception
     */
    @Override
    public int refund() throws Exception {
        synchronized (Lock.ORDER_DEPOSIT_STATUS) {
            return orderDepositService.refundGo();
        }
    }

    @Transactional(rollbackFor = Throwable.class)
    public int refundGo() throws Exception {
        int updatePartialById = 0;
        String userId = TokenUtil.getUserId();
        User member = userDaoImpl.findById(userId);
        if (EEarnestMoneyStatus.NO.getCode() == member.getEarnestMoneyStatus()) {
            ExpUtil.throwEx("未支付诚意金！");
        }
        // 查询订单
        OrderDeposit infoByOrderSn = orderDepositDaoImpl.getInfoByOrderSn(member.getEarnestMoneyOrderNo());

        // 查看订单是否存在
        if (StringUtil.isBlankObject(infoByOrderSn)) {
            ExpUtil.throwEx("订单不存在！");
        }

        // 查看订单是否可以取消
        if (EOrderDepositStatus.RETU_YES.getCode() != infoByOrderSn.getStatus()) {
            ExpUtil.throwEx("订单诚意金已扣除或订单诚意金已退还或订单未付款或已关闭！");
        }

        PostApplyVo postApply = new PostApplyVo();
        postApply.setUserId(userId);
        postApply.setPostsStatus(EPostApplyPostsStatus.AUDIT.getCode());
        postApply.setPostType(EPostType.PART_TIME.getCode());
        int countByDto = postApplyDaoImpl.count(postApply);
        if (countByDto > 0) {
            ExpUtil.throwEx("存在待上岗的职位,不能退诚意金！");
        }

        // 修改订单状态
        OrderDeposit ord = new OrderDeposit();
        ord.updateInit();
        ord.setRefundSn(IdUtil.uuid());
        ord.setRefundTotal(infoByOrderSn.getPayTotal());
        ord.setIsRefunds(ERefundsStatus.ING.getCode());
        ord.setId(infoByOrderSn.getId());
        ord.setStatus(EOrderDepositStatus.CANCEL_YES.getCode());
        updatePartialById = orderDepositDaoImpl.updatePartialById(ord);
        if (updatePartialById < 1) {
            ExpUtil.throwEx("订单状态修改失败！");
        }

        // 修改用户诚意金状态
        User mem = new User();
        mem.setId(member.getId());
        mem.setEarnestMoneyStatus(EEarnestMoneyStatus.NO.getCode());
        int updateStock = userDaoImpl.updateById(mem);
        if (updateStock < 1) {
            ExpUtil.throwEx("修改用户诚意金状态失败！");
        }

        ord.refundInit(infoByOrderSn.getOrderSn(), infoByOrderSn.getOrderTotal(), ord.getRefundSn(),
            ord.getRefundTotal(), infoByOrderSn.getPayType());
        PayUtil.refund(ord, IpConfig.getUrl() + WxConstant.NOTIFY_URL_ORDER_DEPOSIT_CANCEL);
        return updatePartialById;
    }

    /**
     * 退诚意金
     *
     * @return
     * @throws Exception
     */
    @Override
    public int refund(OrderDepositVo orderDeposit) throws Exception {
        synchronized (Lock.ORDER_DEPOSIT_STATUS) {
            return refundGo(orderDeposit);
        }
    }

    @Transactional(rollbackFor = Throwable.class)
    public int refundGo(OrderDepositVo orderDeposit) throws Exception {
        int updatePartialById = 0;
        // 查询订单
        OrderDeposit infoByOrderSn = orderDepositDaoImpl.getInfoByOrderSn(orderDeposit.getOrderSn());
        Integer orderStatus = null;

        // 查看订单是否存在
        if (StringUtil.isBlankObject(infoByOrderSn)) {
            ExpUtil.throwEx("订单不存在！");
        }
        String userId = infoByOrderSn.getMid();
        User member = userDaoImpl.findById(userId);

        // 查看订单是否可以取消
        if (EOrderDepositStatus.RETU_YES.getCode() == infoByOrderSn.getStatus()) {
            orderStatus = EOrderDepositStatus.CANCEL_YES.getCode();
        } else {
            if (EOrderDepositStatus.DEDUCT.getCode() == infoByOrderSn.getStatus()) {
                if (EOrderDepositDeductType.ALL.getCode() == infoByOrderSn.getDeductType()) {
                    ExpUtil.throwEx("订单诚意金已扣除！");
                } else if (EOrderDepositIsToBrokerage.YES.getCode() == infoByOrderSn.getIsToBrokerage()) {
                    ExpUtil.throwEx("订单诚意金已转入余额！");
                } else if (ERefundsStatus.YES.getCode() == infoByOrderSn.getIsRefunds()) {
                    ExpUtil.throwEx("订单诚意金已退款至微信！");
                } else if (ERefundsStatus.ING.getCode() == infoByOrderSn.getIsRefunds()) {
                    ExpUtil.throwEx("订单诚意金正在退款中！");
                }
            } else {
                ExpUtil.throwEx("订单诚意金已退还或订单未付款或已退至余额或已关闭！");
            }
        }

        PostApplyVo postApply = new PostApplyVo();
        postApply.setUserId(userId);
        postApply.setPostsStatus(EPostApplyPostsStatus.AUDIT.getCode());
        postApply.setPostType(EPostType.PART_TIME.getCode());
        int countByDto = postApplyDaoImpl.count(postApply);
        if (countByDto > 0) {
            ExpUtil.throwEx("存在待上岗的职位,不能退诚意金！");
        }

        BigDecimal payTotal = infoByOrderSn.getPayTotal();
        int bigDollarToIntPenny = ArithUtil.bigDollarToIntPenny(payTotal);
        int chu = bigDollarToIntPenny / 2;
        int jian = bigDollarToIntPenny - chu;
        if (chu == jian) {
            payTotal = ArithUtil.intPennyToBigDollar(chu);
        } else if (chu < jian) {
            payTotal = ArithUtil.intPennyToBigDollar(chu + 1);
        } else {
            payTotal = ArithUtil.intPennyToBigDollar(chu);
        }
        // 修改订单状态
        OrderDeposit ord = new OrderDeposit();
        ord.updateInit();
        ord.setRefundSn(IdUtil.uuid());
        ord.setRefundTotal(payTotal);
        ord.setIsRefunds(ERefundsStatus.ING.getCode());
        ord.setId(infoByOrderSn.getId());
        ord.setStatus(orderStatus);
        updatePartialById = orderDepositDaoImpl.updatePartialById(ord);
        if (updatePartialById < 1) {
            ExpUtil.throwEx("订单状态修改失败！");
        }

        // 修改用户诚意金状态
        if (infoByOrderSn.getOrderSn().equals(member.getEarnestMoneyOrderNo())) {
            User mem = new User();
            mem.setId(member.getId());
            mem.setEarnestMoneyStatus(EEarnestMoneyStatus.NO.getCode());
            int updateStock = userDaoImpl.updateById(mem);
            if (updateStock < 1) {
                ExpUtil.throwEx("修改用户诚意金状态失败！");
            }
        }

        ord.refundInit(infoByOrderSn.getOrderSn(), infoByOrderSn.getOrderTotal(), ord.getRefundSn(),
            ord.getRefundTotal(), infoByOrderSn.getPayType());
        PayUtil.refund(ord, IpConfig.getUrl() + WxConstant.NOTIFY_URL_ORDER_DEPOSIT_CANCEL);
        return updatePartialById;
    }

    /**
     * 将用户诚意金转入余额
     *
     * @param id
     * @return
     * @throws Exception
     * @throws IOException
     */
    @Override
    public int toBrokerage(OrderDeposit orderDeposit) throws Exception {
        synchronized (Lock.ORDER_DEPOSIT_STATUS) {
            synchronized (Lock.BALANCE_LOG) {
                return orderDepositService.toBrokerageGo(orderDeposit);
            }
        }
    }

    @Transactional(rollbackFor = Throwable.class)
    public int toBrokerageGo(OrderDeposit orderDeposit) throws Exception {
        String id = orderDeposit.getId();
        OrderDeposit findById = orderDepositDaoImpl.findById(id);

        if (StringUtil.isBlankObject(findById)) {
            ExpUtil.throwEx("诚意金订单不存在！");
        }
        Integer status = findById.getStatus();
        BigDecimal total = null;
        // 查看订单是否可以取消
        if (EOrderDepositStatus.RETU_YES.getCode() == status) {
            total = findById.getPayTotal();
        } else {
            if (EOrderDepositStatus.DEDUCT.getCode() == status) {
                if (EOrderDepositDeductType.ALL.getCode() == findById.getDeductType()) {
                    ExpUtil.throwEx("订单诚意金已扣除！");
                } else if (EOrderDepositIsToBrokerage.YES.getCode() == findById.getIsToBrokerage()) {
                    ExpUtil.throwEx("订单诚意金已转入余额！");
                } else if (ERefundsStatus.YES.getCode() == findById.getIsRefunds()) {
                    ExpUtil.throwEx("订单诚意金已退款至微信！");
                }
                if (EOrderDepositDeductType.HALF.getCode() == findById.getDeductType()) {
                    total = findById.getPayTotal().subtract(findById.getDeductTotal());
                } else {
                    total = new BigDecimal("0");
                }
            } else {
                ExpUtil.throwEx("订单诚意金已退还或订单未付款或已退至余额或已关闭！");
            }
        }

        // 修改订单状态
        OrderDeposit od = new OrderDeposit();
        od.updateInit();
        od.setId(findById.getId());
        od.setIsToBrokerage(EOrderDepositIsToBrokerage.YES.getCode());
        od.setStatus(EOrderDepositStatus.CANCEL_BALANCE_YES.getCode());
        int updatePartialById = orderDepositDaoImpl.updatePartialById(od);
        if (updatePartialById < 1) {
            ExpUtil.throwEx("订单状态修改失败！");
        }

        User member = userDaoImpl.findById(findById.getMid());
        if (StringUtil.isBlankObject(member)) {
            ExpUtil.throwEx("诚意金订单的用户不存在！");
        }

        User m = new User();
        m.setId(member.getId());
        m.setBrokerage(member.getBrokerage().add(total));
        m.setBrokerageSum(member.getBrokerageSum().add(total));
        if (findById.getOrderSn().equals(member.getEarnestMoneyOrderNo())
            && member.getEarnestMoneyStatus() == EEarnestMoneyStatus.YES.getCode()) {
            m.setEarnestMoneyStatus(EEarnestMoneyStatus.NO.getCode());
        }
        updatePartialById = userDaoImpl.updateById(m);
        if (updatePartialById < 1) {
            ExpUtil.throwEx("余额修改失败！");
        }

        BalanceLog balanceLog = new BalanceLog();
        balanceLog.insertInit("诚意金提现至余额", member.getId(), total, member.getBrokerage(), EBalanceType.DEPOSIT.getCode(),
            null, EBalanceTotalType.BALANCE.getCode(), EBalanceCount.ADD.getCode(), findById.getOrderSn(),
            findById.getOrderSn() + "-诚意金提现至余额");
        int insert = balanceLogService.insert(balanceLog);
        if (insert < 1) {
            ExpUtil.throwEx("添加佣金记录失败！");
        }

        // 发送消息
        messageSystemService.send(ConstantMessageSystemTemplate.getOrderDepositToBrokerage(total, m.getUpdateTime()),
            EMessageSystemType.DEPOSIT_CANCEL.getCode(), null, findById.getOrderSn(), member);
        // TODO Auto-generated method stub
        return updatePartialById;
    }

    /**
     * 通过id更新(局部更新)
     *
     * @param orderDeposit
     * @return
     */
    @Override
    public int updateStatus(OrderDepositVo orderDeposit) {
        synchronized (Lock.ORDER_DEPOSIT_STATUS) {
            return orderDepositService.updateStatusGo(orderDeposit);
        }
    }

    /**
     * 通过id更新(局部更新)
     *
     * @param orderDeposit
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    public int updateStatusGo(OrderDepositVo orderDeposit) {
        Integer status = orderDeposit.getStatus();
        OrderDeposit findById = orderDepositDaoImpl.findById(orderDeposit.getId());
        if (StringUtil.isBlankObject(findById)) {
            ExpUtil.throwEx("诚意金订单不存在！");
        }
        if (findById.getStatus().intValue() == status) {
            return 1;
        }

        int updatePartialById = orderDepositDaoImpl.updatePartialById(orderDeposit);
        if (updatePartialById < 1) {
            ExpUtil.throwEx("修改失败，请重试！");
        }

        // 用户诚意金状态
        int userStatus = 0;
        if (status == EOrderDepositStatus.RETU_YES.getCode()) {
            userStatus = EEarnestMoneyStatus.YES.getCode();
        } else if (status == EOrderDepositStatus.CLOSE.getCode()) {
            userStatus = EEarnestMoneyStatus.NO.getCode();
        } else {
            ExpUtil.throwEx("此状态暂不支持修改！");
        }
        User member = userDaoImpl.findById(orderDeposit.getMid());
        User m = new User();
        m.setId(member.getId());
        m.setStatus(userStatus);
        if (StringUtil.isBlankObject(member)) {
            return 1;
        }

        if (findById.getOrderSn().equals(member.getEarnestMoneyOrderNo())) {
            if (member.getEarnestMoneyStatus() == userStatus) {
                return 1;
            }
        } else {
            if (userStatus == EEarnestMoneyStatus.YES.getCode()) {
                Boolean isSetMain = orderDeposit.getIsSetMain();
                if (member.getEarnestMoneyStatus() == EEarnestMoneyStatus.NO.getCode()
                    || (StringUtil.isNotBlankObject(isSetMain) && isSetMain)) {
                    m.setEarnestMoneyOrderNo(findById.getOrderSn());
                }
            }
        }
        updatePartialById = userDaoImpl.updateById(m);
        if (updatePartialById < 1) {
            ExpUtil.throwEx("修改失败，请重试！");
        }
        return updatePartialById;
    }

}